import os
import logging
from http import server

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filemode='w')

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
import other_config
import sys
from handlers import *
from application import Application
from tornado.options import define, options
from other_config import PathConfig


def main(port):
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(port)

    logging.info(f"""
        ************                          ****************
        ****************server {port} is running********************
        ************                          ****************
                
                DOWNLOAD ROOT: {PathConfig.SHARE_PATH}
                
            """)
    tornado.ioloop.IOLoop.instance().start()


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('--directory', '-d', default=os.getcwd(),
                        help='Specify alternative directory '
                             '[default:current directory]')
    parser.add_argument('port', action='store',
                        default=8000, type=int,
                        nargs='?',
                        help='Specify alternate port [default: 8000]')

    command_args = parser.parse_args()
    PathConfig.SHARE_PATH = command_args.directory

    main(port=command_args.port)
